/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package hu.rsc.feeds.client.gui;

import hu.rsc.feeds.client.Feeds;
import hu.rsc.feeds.client.data.Dictionary;
import hu.rsc.feeds.client.data.InitData;
import hu.rsc.feeds.client.data.UserFeed;
import hu.rsc.feeds.client.data.UserInfo;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.user.client.ui.TextBox;

/**
 * 
 * @author arbuckle
 */
public class RegisterPopup extends DialogBox {
    public static final int MIN_USERNAME_LENGTH = 5;
    public static final int MIN_PASSWORD_LENGTH = 5;
    TextBox userName = null;
    PasswordTextBox password = null;
    TextBox mail = null;
    Feeds parent;
    TextBox rowsTb = null;
    TextBox colsTb = null;
    final CheckBox huCb = new CheckBox();
    final CheckBox enCb = new CheckBox();
    final CheckBox needmailCb = new CheckBox();

    public RegisterPopup(final Feeds parent) {
	super(true);
	addStyleName("body");
	setAnimationEnabled(true);
	setText(Dictionary.getEntry(Dictionary.TITLE_REGISTER, this));
	this.parent = parent;
	FlexTable main = new FlexTable();

	this.userName = new TextBox();
	this.userName.setValue(registered() ? Feeds.getUserInfo().getUserName() : null);
	Label nameLb = new Label();
	nameLb.setText(Dictionary.getEntry(Dictionary.LABEL_LOGINNAME, nameLb));
	main.setWidget(0, 0, nameLb);
	main.setWidget(0, 1, this.userName);

	this.password = new PasswordTextBox();
	Label passLb = new Label();
	passLb.setText(Dictionary.getEntry(Dictionary.LABEL_PASSWORD, passLb));
	main.setWidget(1, 0, passLb);
	main.setWidget(1, 1, this.password);

	this.mail = new TextBox();
	this.mail.setValue(registered() ? Feeds.getUserInfo().getEmail() : null);
	Label mailLb = new Label();
	mailLb.setText(Dictionary.getEntry(Dictionary.LABEL_MAIL, mailLb));
	main.setWidget(2, 0, mailLb);
	main.setWidget(2, 1, this.mail);

	this.rowsTb = new TextBox();
	this.rowsTb.setValue(registered() ? Feeds.getUserInfo().getRows().toString() : String.valueOf(UserInfo.DEF_ROWS));
	Label lb = new Label();
	lb.setText(Dictionary.getEntry(Dictionary.LABEL_ROWCOUNT, lb));
	main.setWidget(3, 0, lb);
	main.setWidget(3, 1, this.rowsTb);

	this.colsTb = new TextBox();
	this.colsTb.setValue(registered() ? Feeds.getUserInfo().getCols().toString() : String.valueOf(UserInfo.DEF_COLS));
	lb = new Label();
	lb.setText(Dictionary.getEntry(Dictionary.LABEL_COLCOUNT, lb));
	main.setWidget(4, 0, lb);
	main.setWidget(4, 1, this.colsTb);

	Label langLb = new Label();
	langLb.addStyleName("formtitle");
	langLb.setText(Dictionary.getEntry(Dictionary.LABEL_LANG, langLb));
	main.setWidget(5, 0, langLb);
	main.getFlexCellFormatter().setColSpan(5, 0, 2);

	String lang = Feeds.getLanguage();

	this.enCb.setText(Dictionary.getEntry(Dictionary.LABEL_LANG_EN, this.enCb));
	if (lang.equals(UserInfo.LANG_EN)) {
	    this.enCb.setValue(true);
	}
	main.setWidget(6, 0, this.enCb);

	this.huCb.setText(Dictionary.getEntry(Dictionary.LABEL_LANG_HU, this.huCb));
	if (lang.equals(UserInfo.LANG_HU)) {
	    this.huCb.setValue(true);
	}
	main.setWidget(6, 1, this.huCb);

	this.enCb.addClickHandler(new ClickHandler() {
	    @Override
	    public void onClick(ClickEvent event) {
		if (RegisterPopup.this.enCb.getValue()) {
		    RegisterPopup.this.huCb.setValue(false);
		    RegisterPopup.this.parent.changeLanguage(UserInfo.LANG_EN);
		}
	    }
	});
	this.huCb.addClickHandler(new ClickHandler() {
	    @Override
	    public void onClick(ClickEvent event) {
		if (RegisterPopup.this.huCb.getValue()) {
		    RegisterPopup.this.enCb.setValue(false);
		    RegisterPopup.this.parent.changeLanguage(UserInfo.LANG_HU);
		}
	    }
	});

	if (Feeds.getUserInfo() != null) {
	    this.enCb.setValue(false);
	    this.huCb.setValue(false);
	    if (Feeds.getUserInfo().getLang().equals("en")) {
		this.enCb.setValue(true);
	    } else if (Feeds.getUserInfo().getLang().equals("hu")) {
		this.huCb.setValue(true);
	    } else {
		this.enCb.setValue(true);
	    }
	}

	Label nmLabel = new Label();
	nmLabel.setText(Dictionary.getEntry(Dictionary.LABEL_NEED_NEWFEED_MAIL, nmLabel));
	main.setWidget(7, 0, nmLabel);
	this.needmailCb.setValue(Feeds.getUserInfo() != null && Feeds.getUserInfo().getNeedmail() != null ? Feeds.getUserInfo().getNeedmail() : new Boolean(
		true));
	main.setWidget(7, 1, this.needmailCb);

	Button submit = new Button();
	submit.addStyleName("button");
	submit.setText(Dictionary.getEntry(registered() ? Dictionary.BUTTON_EDIT_PROFILE : Dictionary.BUTTON_REGISTER, submit));
	submit.addClickHandler(new ClickHandler() {

	    @Override
	    public void onClick(ClickEvent event) {
		Object result = check();
		if (result instanceof String) {
		    Window.alert(result.toString());
		    return;
		}
		final UserInfo userInfo = (UserInfo) result;
		parent.service.register(userInfo, new AsyncCallback<InitData>() {

		    @Override
		    public void onFailure(Throwable caught) {
			Window.alert(Dictionary.getEntry(Dictionary.MESSAGE_REG_ERROR, null));
		    }

		    @Override
		    public void onSuccess(InitData result) {
			if (result == null) {
			    Window.alert(Dictionary.getEntry(Dictionary.MESSAGE_EXISTED_USER, null));
			} else {
			    finishRegistration(result);
			}
		    }
		});
	    }
	});
	main.setWidget(8, 0, submit);
	main.getFlexCellFormatter().setColSpan(8, 0, 2);

	main.addStyleName("popup");
	main.addStyleName("border");
	main.addStyleName("form");
	main.getFlexCellFormatter().addStyleName(0, 0, "formlabel");
	main.getFlexCellFormatter().addStyleName(1, 0, "formlabel");
	main.getFlexCellFormatter().addStyleName(2, 0, "formlabel");
	main.getFlexCellFormatter().addStyleName(3, 0, "formlabel");
	main.getFlexCellFormatter().addStyleName(4, 0, "formlabel");
	main.getFlexCellFormatter().addStyleName(7, 0, "formlabel");
	this.userName.addStyleName("forminput");
	this.password.addStyleName("forminput");
	this.mail.addStyleName("forminput");
	this.rowsTb.addStyleName("forminput");
	this.colsTb.addStyleName("forminput");
	this.needmailCb.addStyleName("forminput");
	main.getFlexCellFormatter().addStyleName(6, 0, "formlabel");
	this.huCb.addStyleName("forminput");
	main.getFlexCellFormatter().addStyleName(8, 0, "buttonpanel");

	setWidget(main);
    }

    public Object check() {
	StringBuilder sb = new StringBuilder();
	if (this.userName.getValue() == null || this.userName.getValue().length() < MIN_USERNAME_LENGTH) {
	    sb.append(Dictionary.getEntry(Dictionary.MESSAGE_USERNAME_LENGTH, null) + "\r\n");
	}
	if (!registered() && (this.password.getValue() == null || this.password.getValue().length() < MIN_PASSWORD_LENGTH)) {
	    sb.append(Dictionary.getEntry(Dictionary.MESSAGE_PASSWORD_LENGTH, null) + "\r\n");
	}
	if (registered() && this.password.getValue() != null && this.password.getValue().length() != 0
		&& this.password.getValue().length() < MIN_PASSWORD_LENGTH) {
	    sb.append(Dictionary.getEntry(Dictionary.MESSAGE_PASSWORD_LENGTH, null) + "\r\n");
	}
	if (this.mail.getValue() == null || this.mail.getValue().length() == 0) {
	    sb.append(Dictionary.getEntry(Dictionary.MESSAGE_MAIL_REQUIRED, null) + "\r\n");
	}
	Long rows = UserInfo.DEF_ROWS;
	Long cols = UserInfo.DEF_COLS;
	try {
	    rows = Long.parseLong(this.rowsTb.getValue());
	} catch (Exception e) {
	}
	try {
	    cols = Long.parseLong(this.colsTb.getValue());
	} catch (Exception e) {
	}
	if (!checkColNum(cols.intValue())) {
	    sb.append(Dictionary.getEntry(Dictionary.MESSAGE_INVALID_ROWNUMBER, null) + "\r\n");
	}
	if (!checkRowNum(rows.intValue())) {
	    sb.append(Dictionary.getEntry(Dictionary.MESSAGE_INVALID_COLNUMBER, null) + "\r\n");
	}

	if (sb.length() > 0) {
	    return sb.toString();
	}
	UserInfo userInfo = registered() ? Feeds.getUserInfo() : new UserInfo();
	userInfo.setUserName(this.userName.getValue());
	userInfo.setPassword(this.password.getValue());
	userInfo.setEmail(this.mail.getValue());
	if (this.enCb.getValue()) {
	    userInfo.setLang(UserInfo.LANG_EN);
	}
	if (this.huCb.getValue()) {
	    userInfo.setLang(UserInfo.LANG_HU);
	}

	userInfo.setRows(rows);
	userInfo.setCols(cols);
	userInfo.setNeedmail(this.needmailCb.getValue() != null ? this.needmailCb.getValue() : new Boolean(true));
	return userInfo;
    }

    public void finishRegistration(InitData id) {
	this.parent.refresh(id);
	this.setVisible(false);
    }

    private boolean checkColNum(int num) {
	if (num < 1 || num > 10) {
	    return false;
	}
	for (UserFeed uf : this.parent.userFeeds.values()) {
	    if (uf.getY() + 1 > num) {
		return false;
	    }
	}
	return true;
    }

    private boolean checkRowNum(int num) {
	if (num < 1 || num > 10) {
	    return false;
	}
	for (UserFeed uf : this.parent.userFeeds.values()) {
	    if (uf.getX() + 1 > num) {
		return false;
	    }
	}
	return true;
    }

    private boolean registered() {
	return Feeds.getUserInfo() != null && Feeds.getUserInfo().getId() != null && Feeds.getUserInfo().getId() > 0l;
    }
}
